package com.tongtech.proxy.core.sync.codec;

import java.util.*;

public class connectSshu {
    private static final int MAZE_SIZE = 10;
    private static final int[][] DIRECTIONS = {
            {0, 1},
            {1, 0},
            {0, -1},
            {-1, 0}
    };

    public static void main(String[] args) {
        int[][] maze = generateMaze();
        printMaze(maze);
        List<int[]> path = solveMaze(maze);
        System.out.println("Path found:");
        for (int[] cell : path) {
            System.out.println(Arrays.toString(cell));
        }
    }

    private static int[][] generateMaze() {
        Random random = new Random();
        int[][] maze = new int[MAZE_SIZE][MAZE_SIZE];
        for (int i = 0; i < MAZE_SIZE; i++) {
            for (int j = 0; j < MAZE_SIZE; j++) {
                maze[i][j] = random.nextInt(2); // Randomly generate 0 or 1
            }
        }
        return maze;
    }

    private static List<int[]> solveMaze(int[][] maze) {
        Stack<int[]> stack = new Stack<>();
        List<int[]> path = new ArrayList<>();
        boolean[][] visited = new boolean[MAZE_SIZE][MAZE_SIZE];
        stack.push(new int[]{0, 0}); // Start from top-left corner
        while (!stack.isEmpty()) {
            int[] currentCell = stack.pop();
            int row = currentCell[0];
            int col = currentCell[1];
            path.add(currentCell);
            visited[row][col] = true;
            if (row == MAZE_SIZE - 1 && col == MAZE_SIZE - 1) {
                break; // Reached the end
            }
            List<int[]> neighbors = getNeighbors(row, col, maze);
            for (int[] neighbor : neighbors) {
                int nextRow = neighbor[0];
                int nextCol = neighbor[1];
                if (!visited[nextRow][nextCol]) {
                    stack.push(neighbor);
                }
            }
        }
        return path;
    }

    private static List<int[]> getNeighbors(int row, int col, int[][] maze) {
        List<int[]> neighbors = new ArrayList<>();
        for (int[] direction : DIRECTIONS) {
            int nextRow = row + direction[0];
            int nextCol = col + direction[1];
            if (nextRow >= 0 && nextRow < MAZE_SIZE && nextCol >= 0 && nextCol < MAZE_SIZE && maze[nextRow][nextCol] == 0) {
                neighbors.add(new int[]{nextRow, nextCol});
            }
        }
        return neighbors;
    }

    private static void printMaze(int[][] maze) {
        for (int[] row : maze) {
            System.out.println(Arrays.toString(row));
        }
    }
}

